package com.xyzk.diagnose.tools;

import com.xyzk.diagnose.activity.bluetooth.BluetoothService;

public class Communication {
	private static final String TAG = "Communication";
	public static boolean exit = false;
	private static  Communication m_com;
	BluetoothService bcom = null;
	JCommunication Jcom = null;
	public LogFile m_log = null;

	private Communication()
	{
		m_log = LogFile.getInstance();
		if(Common.Communication == 0) //blue
			bcom = BluetoothService.getInstance();
		else if(Common.Communication == 1) //cable
			Jcom = JCommunication.getInstance();
		if(m_log != null)
			m_log.PrintLog(TAG + " mode:" + Common.Communication);
	}
	public synchronized static Communication getInstance(){
		return m_com == null ? m_com = new Communication() : m_com;
	}
	//can
	public int CanOne2OneUDS(int Preq,int Pres,byte [] Send,int Plen,byte [] Recv,int Perror,int Pmaxtime,int Pfreq)
	{
		try{
			if(Common.Communication == 0)
				return bcom.CanOne2OneUDS(Preq, Pres, Send, Plen, Recv, Perror, Pmaxtime, Pfreq);
			else
				return Jcom.CanOne2OneUDS(Preq, Pres, Send, Plen, Recv, Perror, Pmaxtime, Pfreq);
		}catch (Exception e){
			m_log.PrintLog("错误异常："+e);
			throw new RuntimeException(e);
		}
	}
	public int CanSendOneOnly(int SendId,byte[] send,int Plen)
	{
		if(Common.Communication == 0)
			return bcom.canSendOneOnly(SendId, send, Plen);
		else
			return Jcom.CanSendOneOnly(SendId, send, Plen);
	}
//	public int canSendAndRecvN2N(byte[] send, byte[] recv, int maxtime){
//		if(Common.Communication == 0)
//			return bcom.canSendAndRecvN2N(send, send, maxtime);
//		else return 1;
//	}
	public int CanSendAndRecvO2O_Spec(int Psid,int Prid,byte [] Send,int Plen,byte [] Recv,int Psize,int errorcode,int maxtime)
	{
		if(Common.Communication == 0)
			return bcom.CanSendAndRecvO2O_Spec(Psid,Prid,Send,Plen,Recv,Psize,errorcode,maxtime);
		else
			return Jcom.CanSendAndRecvO2O_Spec(Psid,Prid,Send,Plen,Recv,Psize,errorcode,maxtime);
	}
	public String ReadBoxVersion()
	{
		byte [] v_buf = new byte[32];
		byte [] v_soft = new byte[32];
		for(int i = 0; i < 32; i ++)
		{
			v_buf[i] = 0;
			v_soft[i] = 0;
		}

		if(Common.Communication == 0)
		{
			if(bcom.GetSerialNum(	v_buf, 32, v_soft) == true)
				return new String(v_buf) + "_" + new String(v_soft);
			else
				return "";
		}
		else
			return Jcom.ReadVersion();
	}
	public boolean SetCommunTimer(int P1,int P2,int P3,int P4)
	{
		if(Common.Communication == 0)
			return bcom.setCommunTimer(P1, P2, P3, P4);
		else
			return Jcom.SetCommunTimer(P1, P2, P3, P4);
	}
	public boolean SetCanBaud(long baud)
	{
		if(Common.Communication == 0)
			return bcom.setCanBaud(baud);
		else
			return Jcom.SetCanBaud(baud);
	}
	public boolean SetCanMode(int Pmode)
	{
		if(Common.Communication == 0)
			return bcom.setCanMode(Pmode);
		else
			return Jcom.SetCanMode(Pmode);
	}
	public boolean SetCanFilter(byte [] Pbuf,int num)
	{
		if(Common.Communication == 0)
			return bcom.SetCanFilter(Pbuf,num);
		else
			return Jcom.SetCanFilter(Pbuf,num);
	}
	public boolean SetCanAutoFrame(byte [] Pbuf,int Plen)
	{
		if(Common.Communication == 0)
			return bcom.setCanAutoFrame(Pbuf,Plen);
		else
			return Jcom.SetCanAutoFrame(Pbuf,Plen);
	}
	//设置CAN自动回复
	public boolean SetStandCanAutoFrame(int Pid,byte [] Pbuf,int Plen)
	{
		if(Common.Communication == 0)
			return bcom.SetStandCanAutoFrame(Pid,Pbuf,Plen);
		else
			return Jcom.SetStandCanAutoFrame(Pid,Pbuf,Plen);
	}
	public String GetLogfileName()
	{
		if(Common.Communication == 0)
			return bcom.GetLogfileName();
		else
			return Jcom.GetLogfileName();
	}
	public String getFilePaths()
	{
		if(Common.Communication == 0)
			return bcom.getFilePaths();
		else
			return Jcom.getFilePaths();
	}
	public void saveLogData(int path,String data){
		if(Common.Communication == 0)
			bcom.saveLogData(path,data);
		else
			Jcom.saveLogData(path,data);
	}
}
